package cn.tedu.lucky.draw.server.job.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {
    /**
     * 作业启动器
     */
    @Autowired
    private JobLauncher jobLauncher;

    /**
     * Job 构造工厂
     * 用于构建Job对象
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * Step 构造工厂
     * 用于构建Step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    //构造一个Job对象
    @Bean
    public Job job(){
        return jobBuilderFactory.get("hello-job")
                .start(step())
                .build();
    }

    //构造一个step对象
    @Bean
    public Step step() {
        //tasklet 指定 step 执行逻辑，类似于 Thread 操作
        return stepBuilderFactory.get("step") //为当前步骤取一个名字
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("Hello,Spring Batch！");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

}
